//
//  DTJavascriptCallManager.m
//  PersonalManager
//
//  Created by user on 16/7/29.
//  Copyright © 2016年 Haiyun.Qian. All rights reserved.
//

#import "DTJavascriptCallManager.h"
#import "DTJavascriptDevice.h"
@interface DTJavascriptCallManager ()
@property (nonatomic, strong) NSString *test;

@end

@implementation DTJavascriptCallManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.device = [[DTJavascriptDevice alloc]init];
        self.biz = [[DTJavascriptBiz alloc]init];
        self.UIController = [[DTJavascriptUI alloc]init];
    }
    return self;
}

-(void)exectue:(DTJavascriptCommand *)command{
    
    NSString *moduleName = command.moduleName;
    NSString *className  = command.className;
    NSString *methodName = command.methodName;
    
    if (methodName == nil || className == nil) {
        [command failedResponse:1 andmessage:@"invilide command."];
    }
    //self.moduleObj = [[DTjavascripteObject alloc] init];
    self.moduleObj = [self valueForKey:moduleName];
    
    if (self.moduleObj) {
        if (methodName == nil) {
            [self callMethod:self.moduleObj andMethodName:className andCommandParam:command];
        } else {
            //并且返回了一个实例引用给classObj
            NSObject *classObj = [self.moduleObj valueForKey:className];
            if (classObj) {
                [self callMethod:classObj andMethodName:command.methodName andCommandParam:command];
            } else {
                [command failedResponse:1 andmessage:@"invilide class name."];
            }
        }
    }else{
        [command failedResponse:1 andmessage:@"invilide module name."];
    }

    
}
-(void)callMethod:(NSObject *)classObj andMethodName:(NSString *)methodName andCommandParam:(DTJavascriptCommand *)commandParam{
    NSString *actionName = [NSString stringWithFormat:@"%@:",methodName];
    SEL sel = NSSelectorFromString(actionName);
    if (sel != nil) {
        if ([classObj respondsToSelector:sel]) {
            
            [classObj performSelector:sel withObject:commandParam afterDelay:0.0];
        }else {
            [commandParam failedResponse:1 andmessage:@"invilide method name."];
        }
    }
}

@end


